$().ready(function() {
    initQueryTime();
});
var prefix = "/car/black"
function initQueryTime() {
    //定义locale汉化插件
    var locale = {
        "format": 'YYYY-MM-DD',
        "separator": " - ",
        "applyLabel": "确定",
        "cancelLabel": "取消",
        "fromLabel": "起始时间",
        "toLabel": "结束时间'",
        "customRangeLabel": "自定义",
        "weekLabel": "W",
        "daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
        "monthNames": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        "firstDay": 1
    };
    //初始化显示当前时间
    $('#daterange-btn span').html(moment().subtract('month', 0).format('YYYY-MM-DD') + ' - ' + moment().add('month',12).format('YYYY-MM-DD'));
    //日期控件初始化
    $('#daterange-btn').daterangepicker(
        {
            'locale': locale,
            //汉化按钮部分
            ranges: {
                '今日': [moment(), moment()],
                '昨日': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                '最近7日': [moment().subtract(6, 'days'), moment()],
                '最近30日': [moment().subtract(29, 'days'), moment()],
                '本月': [moment().startOf('month'), moment().endOf('month')],
                '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            },
            startDate: moment().subtract('month', 0),
            endDate: moment().add('month', 12)
        },
        function (start, end) {
            $('#daterange-btn span').html(start.format('YYYY-MM-DD') + ' - ' + end.format('YYYY-MM-DD'));
        }
    );
};
function save() {
    var carNum = $('#carNum').val().trim();
    var type = $('#type').val().trim();
    var communityId = $('#communityId').val().trim();
    var timeRange = $('#timeRange').html().trim();
    if(carNum == ''){
        parent.layer.alert('请输入车牌号');
        return;
    }
    if(communityId == '' || communityId == '-1'){
        parent.layer.alert('请选择绑定的小区');
        return;
    }
    var startTime = timeRange.split(' - ')[0]+" 00:00:00";
    var endTime = timeRange.split(' - ')[1]+" 23:59:59";
    var params={
        carNum:carNum,
        type:type,
        communityId:communityId,
        startTime:startTime,
        endTime:endTime
    };
    $.ajaxSetup({
        contentType:"application/json"
    });
	$.ajax({
		cache : true,
		type : "POST",
		url : "/car/black/save",
		data : JSON.stringify(params),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("新增黑名单成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}

function choose() {
    layer.open({
        type : 2,
        title : '选择小区',
        maxmin : true,
        shadeClose : false, // 点击遮罩关闭层
        area : [ '600px', '480px' ],
        content : prefix + '/choose' // iframe的url
    });
}

function setComunityInfo(id,name){
    $("#communityId").val(id);
    $("#communityName").val(name);
}